<template>
  <div class="main-member">
    <!-- 弹出框 -->
    <el-dialog
      title="收货地址"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <div class="edit-address">
        <div class="item">
          <input
            type="text"
            placeholder="请输入收货人姓名"
            v-model="informations.name"
            @blur="checkName"
          />
          <div class="tips error" v-show="nameCheck">请输入姓名</div>
        </div>
        <div class="item">
          <div class="flex">
            <el-select v-model="selectProvince" placeholder="请选择省">
              <el-option
                v-for="item in provinces"
                :key="item.key"
                :label="item.label"
                :value="item.cityname"
              >
              </el-option>
            </el-select>
            <el-select v-model="selectcity" placeholder="请选择市">
              <el-option
                v-for="item in citys"
                :key="item.key"
                :label="item.label"
                :value="item.cityname"
              >
              </el-option>
            </el-select>
            <el-select v-model="selectdistrict" placeholder="请选择区">
              <el-option
                v-for="item in districts"
                :key="item.key"
                :label="item.label"
                :value="item.cityname"
              >
              </el-option>
            </el-select>
          </div>
          <div class="tips error" v-show="cityCheck">请选择城市</div>
        </div>
        <div class="item">
          <input
            type="text"
            placeholder="请输入收货地址"
            v-model="informations.desc"
            @blur="checkDesc"
          />
          <div class="tips error" v-show="descCheck">请输入地址</div>
        </div>
        <div class="item">
          <input
            type="text"
            placeholder="请输入手机号码"
            v-model="informations.phone"
            @blur="checkPhone"
          />
          <div class="tips error" v-show="phoneCheck">请输入手机号</div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addMyAddress">确 定</el-button>
      </span>
    </el-dialog>
    <Header>
      <template v-slot:title>
        <div class="row member">
          <div class="logo fl">
            <router-link to="/">
              <img src="../assets/image/mall_index/ia_100000001.png" alt="" />
            </router-link>
            <router-link to="/order">
              <div class="title">首页</div>
            </router-link>
            <el-menu
              class="el-menu-demo"
              mode="horizontal"
              @select="handleSelect1"
            >
              <el-submenu index="2">
                <template slot="title">账户设置</template>
                <el-menu-item index="个人信息">个人信息</el-menu-item>
                <el-menu-item index="地址管理">地址管理</el-menu-item>
                <el-menu-item index="账户安全">账户安全</el-menu-item>
                <el-menu-item index="账号关联">账号关联</el-menu-item>
              </el-submenu>
            </el-menu>
          </div>
          <div class="search member fr">
            <form action="">
              <input
                type="text"
                v-model="ipt"
                @keydown.enter.prevent="searchProd(ipt)"
              />
              <button type="button" @click="searchProd(ipt)">
                <i class="iconfont icon-sousuo"></i>
              </button>
            </form>
          </div>
        </div>
      </template>
    </Header>
    <div class="container order">
      <div class="menu">
        <el-tree
          :data="data"
          :props="defaultProps"
          @node-click="handleNodeClick"
          default-expand-all
        ></el-tree>
      </div>
      <div class="main-content">
        <h2 class="main-title">{{ title }}</h2>
        <div
          class="position-content el-card__body"
          v-show="title == '个人信息'"
        >
          <div class="userImg">
            <div class="img">
              <label>
                <img :src="touxiang" alt="" />
                <input type="file" @change="handleChange" />
              </label>
            </div>
            <div class="tips">
              <div class="tips1">点击头像即可编辑</div>
              <div class="tips1">允许的文件类型为JPG、GIF或者PNG</div>
              <div class="tips1">最大文件尺寸为1MB</div>
            </div>
          </div>
          <el-row :gutter="20">
            <el-col :span="8">
              <div class="name">
                <label>
                  <div class="trueName">姓名</div>
                  <el-input></el-input>
                </label>
              </div>
            </el-col>
            <el-col :span="14" :offset="2">
              <div>
                性别
                <el-radio v-model="info.sex" label="1">男</el-radio>
                <el-radio v-model="info.sex" label="2">女</el-radio>
              </div>
            </el-col>
            <el-col :span="8">
              <div class="name">
                <label>
                  <div class="birthday">生日</div>
                  <div class="block">
                    <el-date-picker
                      v-model="value1"
                      type="date"
                      placeholder="选择日期"
                    >
                    </el-date-picker>
                  </div>
                </label>
              </div>
            </el-col>
            <el-col :span="14" :offset="2">
              <div class="area">
                <div class="block">
                  <span class="demonstration">所在地区</span>
                  <el-cascader :options="citys"></el-cascader>
                </div>
              </div>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col>
              <div class="huxing">家庭户型</div>
              <el-radio v-model="radio" label="1">普通单元式</el-radio>
              <el-radio v-model="radio" label="2">公寓式</el-radio>
              <el-radio v-model="radio" label="3">花园洋房式</el-radio>
              <el-radio v-model="radio" label="4">跃层式</el-radio>
              <el-radio v-model="radio" label="5">复式</el-radio>
            </el-col>
            <el-col>
              <div class="huxing">家庭面积</div>
              <el-select v-model="value5" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-col>
            <el-col>
              <div class="huxing">家庭成员</div>
              <el-checkbox-group v-model="checkList">
                <el-checkbox label="个人"></el-checkbox>
                <el-checkbox label="父母"></el-checkbox>
                <el-checkbox label="爱人"></el-checkbox>
                <el-checkbox label="小孩"></el-checkbox>
                <el-checkbox label="宠物"></el-checkbox>
              </el-checkbox-group>
            </el-col>
            <el-col :span="8">
              <div class="name">
                <label>
                  <div class="trueName">邮箱</div>
                  <el-input></el-input>
                </label>
              </div>
            </el-col>
          </el-row>
          <el-button>提交</el-button>
        </div>
        <div
          class="position-content el-card__body"
          v-show="title == '地址管理'"
        >
          <div class="addAddress" @click="dialogVisible = true">
            新增收货地址
          </div>
          <el-table :data="addressList" style="width: 100%">
            <el-table-column prop="name" label="名字" width="180">
            </el-table-column>
            <el-table-column prop="phone" label="手机号" width="180">
            </el-table-column>
            <el-table-column prop="area_name" label="所在地区">
              <template v-slot="scope">
                <div class="cell">
                  {{ scope.row.area_name.split("-")[0] }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="desc" label="详细地址"> </el-table-column>
            <el-table-column label="操作">
              <template v-slot="scope">
                <span @click="editAddress(scope.row)">编辑</span> |
                <span @click="del(scope.row.id)">删除</span>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="position-content" v-show="title == '账户安全'">
          <div class="el-card__body">
            <div class="security__item">
              <div class="el-row">
                <div class="el-col el-col-3 el-col-offset-2">
                  <div class="security__name">
                    <i class="el-icon-circle-check-outline text-red"></i
                    >登录密码
                  </div>
                </div>
                <div class="el-col el-col-13 el-col-offset-1">
                  <div class="text-gray">用于保护账号信息和登录安全</div>
                </div>
                <div class="el-col el-col-3 el-col-offset-2">
                  <a href="javascript:void(0)" class="red">修改</a>
                </div>
              </div>
            </div>
            <div class="security__item" v-if="user.phone">
              <div class="el-row">
                <div class="el-col el-col-3 el-col-offset-2">
                  <div class="security__name">
                    <i class="el-icon-circle-check-outline text-red"></i
                    >绑定手机
                  </div>
                </div>
                <div class="el-col el-col-13 el-col-offset-1">
                  <div class="text-gray">
                    您绑定的手机：{{ user.phone | saveNum }}
                    若已丢失或停用，请立即更换
                  </div>
                </div>
                <div class="el-col el-col-3 el-col-offset-2">
                  <a href="javascript:void(0)" class="red">修改</a>
                </div>
              </div>
            </div>
            <div class="el-dialog__wrapper" style="display: none">
              <div
                role="dialog"
                aria-modal="true"
                aria-label="修改密码"
                class="el-dialog"
                style="margin-top: 15vh; width: 450px"
              >
                <div class="el-dialog__header">
                  <span class="el-dialog__title">修改密码</span
                  ><button
                    type="button"
                    aria-label="Close"
                    class="el-dialog__headerbtn"
                  >
                    <i class="el-dialog__close el-icon el-icon-close"></i>
                  </button>
                </div>
                <!----><!---->
              </div>
            </div>
            <div class="el-dialog__wrapper" style="display: none">
              <div
                role="dialog"
                aria-modal="true"
                aria-label="绑定手机"
                class="el-dialog"
                style="margin-top: 15vh; width: 350px"
              >
                <div class="el-dialog__header">
                  <span class="el-dialog__title">绑定手机</span
                  ><button
                    type="button"
                    aria-label="Close"
                    class="el-dialog__headerbtn"
                  >
                    <i class="el-dialog__close el-icon el-icon-close"></i>
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div
          class="position-content el-card__body"
          v-show="title == '账号关联'"
        >
          <div class="item">
            <div class="left">
              <div class="img">
                <img src="../assets/image/other/qq.png" alt="" />
              </div>
              QQ
            </div>
            <div class="right">
              <span> 未绑定&gt; </span>
            </div>
          </div>
          <div class="item">
            <div class="left">
              <div class="img">
                <img src="../assets/image/other/wechat.png" alt="" />
              </div>
              微信
            </div>
            <div class="right">
              <span> 未绑定&gt; </span>
            </div>
          </div>
          <div class="item">
            <div class="left">
              <div class="img">
                <img src="../assets/image/other/pay.png" alt="" />
              </div>
              支付宝
            </div>
            <div class="right">
              <span> 未绑定&gt; </span>
            </div>
          </div>
          <div class="item">
            <div class="left">
              <div class="img">
                <img src="../assets/image/other/weibo.png" alt="" />
              </div>
              微博
            </div>
            <div class="right">
              <span> 未绑定&gt; </span>
            </div>
          </div>
          <div class="item">
            <div class="left">
              <div class="img">
                <img src="../assets/image/other/baidu.png" alt="" />
              </div>
              百度
            </div>
            <div class="right">
              <span> 未绑定&gt; </span>
            </div>
          </div>
          <div class="item">
            <div class="left">
              <div class="img">
                <img src="../assets/image/other/wechat.png" alt="" />
              </div>
              微信公众号
            </div>
            <div class="right">
              <span> 未绑定&gt; </span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 底部 -->
    <Footer></Footer>
    <!-- 侧边咨询客服按钮 -->
    <consult-serve></consult-serve>
  </div>
</template>

<script>
import {
  getCity,
  getAddress,
  getUser,
  delAddress,
  addAddress,
  updateAddress,
} from "../assets/js/request.js";
import { mapActions } from "vuex";
import Header from "../components/Header.vue";
import Footer from "../components/Footer.vue";
import ConsultServe from "../components/ConsultServe.vue";
export default {
  components: {
    Header,
    Footer,
    ConsultServe,
  },
  data() {
    return {
      addressList: [],
      ipt: "",
      activeIndex: "1",
      title: "个人信息",
      data: [
        {
          label: "账户设置",
          children: [
            {
              label: "个人信息",
            },
            {
              label: "地址管理",
            },
            {
              label: "账户安全",
            },
            {
              label: "账号关联",
            },
          ],
        },
      ],
      defaultProps: {
        children: "children",
        label: "label",
      },
      titleIndex: 1,
      touxiang:
        "https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png",
      //单选 性别
      info: {
        name: "",
        sex: "1",
        birthday: "",
        area: "",
      },
      value1: "",
      //所有城市
      citys: [],
      //单选户型
      radio: "1",
      options: [
        {
          value: "65平米以下",
          label: "65平米以下",
        },
        {
          value: "200平米以上",
          label: "200平米以上",
        },
        {
          value: "65-90平米",
          label: "65-90平米",
        },
        {
          value: "90-120平米",
          label: "90-120平米",
        },
        {
          value: "120-150平米",
          label: "120-150平米",
        },
        {
          value: "150-200平米",
          label: "150-200平米",
        },
      ],
      value5: "",
      checkList: [],
      user: {},
      nameCheck: false,
      cityCheck: false,
      descCheck: false,
      phoneCheck: false,
      dialogVisible: false,
      //省份数组
      provinces: [],
      //城市数组
      citys: [],
      //区数组
      districts: [],
      //选择的地址的id集合
      selectIds: [],
      //选择的省份
      selectProvince: "",
      //选择的城市
      selectcity: "",
      //选择的区
      selectdistrict: "",
      //地址信息
      informations: {
        name: "",
        phone: "",
        area: ``,
        area_name: "",
        desc: "",
      },
      //判断是修改地址还是新增地址
      edit: false,
      editId: "",
    };
  },
  watch: {
    selectProvince(newval, oldval) {
      if (newval) {
        this.citys = this.provinces.filter((ele) => {
          this.selectIds[0] = ele.id;
          return ele.cityname == newval;
        })[0].children;
      }
    },
    selectcity(newval, oldval) {
      if (newval) {
        this.districts = this.citys.filter((ele) => {
          this.selectIds[1] = ele.id;
          return ele.cityname == newval;
        })[0].children;
      }
    },
    selectdistrict(newval, oldval) {
      if (newval) {
        this.districts.forEach((ele) => {
          if (ele.cityname == newval) {
            this.selectIds[2] = ele.id;
          }
        });
      }
    },
  },
  methods: {
    checkName() {
      if (this.informations.name == "") {
        this.nameCheck = true;
      } else {
        this.nameCheck = false;
      }
    },
    checkCity() {
      if (
        this.selectProvince == "" ||
        this.selectcity == "" ||
        this.selectdistrict == ""
      ) {
        this.cityCheck = true;
      } else {
        this.cityCheck = false;
      }
    },
    checkDesc() {
      if (this.informations.desc == "") {
        this.descCheck = true;
      } else {
        this.descCheck = false;
      }
    },
    checkPhone() {
      if (this.informations.phone == "") {
        this.phoneCheck = true;
      } else {
        this.phoneCheck = false;
      }
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
          this.edit = false;
          this.clearEdit();
        })
        .catch((_) => {});
    },
    addMyAddress() {
      let flag = true;
      for (let key in this.informations) {
        flag = this.informations[key] == "" ? !flag : flag;
      }
      if (flag) {
        this.informations.area = this.selectIds.join(",");
        this.informations.area_name =
          this.selectProvince +
          this.selectcity +
          this.selectdistrict +
          `-${this.selectProvince},${this.selectcity},${this.selectdistrict}`;
        if (this.edit) {
          let data = {};
          data.name = this.informations.name;
          data.phone = this.informations.phone;
          data.area = this.informations.area;
          data.desc = this.informations.desc;
          data.area_name = this.informations.area_name;
          updateAddress(this.editId, data).then(() => {
            this.dialogVisible = false;
            this.edit = false;
            getAddress().then((res) => {
              this.addressList = res;
            });
            this.clearEdit();
          });
        } else {
          addAddress(this.informations).then(() => {
            this.dialogVisible = false;
            getAddress().then((res) => {
              this.addressList = res;
            });
            this.clearEdit();
          });
        }
      }
    },
    //修改地址信息
    editAddress(item) {
      this.edit = true;
      this.editId = item.id;
      this.informations.name = item.name;
      this.informations.phone = item.phone;
      this.informations.desc = item.desc;
      this.selectProvince = item.area_name.split("-")[1].split(",")[0];
      this.selectcity = item.area_name.split("-")[1].split(",")[1];
      this.selectdistrict = item.area_name.split("-")[1].split(",")[2];
      this.dialogVisible = true;
    },
    //删除地址
    del(id) {
      this.$confirm("确定要删除么?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          delAddress(id).then(() => {
            getAddress().then((res) => {
              this.addressList = res;
            });
          });
        })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //清空地址输入框
    clearEdit() {
      for (let key in this.informations) {
        this.informations[key] = "";
      }
      this.selectProvince = "";
      this.selectcity = "";
      this.selectdistrict = "";
    },
    handleNodeClick(data) {
      this.title = data.label;
      sessionStorage.setItem("useInfo", this.title);
    },
    handleSelect(key, keyPath) {
      this.titleIndex = key;
    },
    handleSelect1(key, keyPath) {
      this.title = key;
      sessionStorage.setItem("useInfo", this.title);
    },
    handleChange(e) {
      let fileData = e.target.files[0];
      const windowURL = window.URL || window.webkitURL;
      this.touxiang = windowURL.createObjectURL(fileData);
    },
  },
  beforeRouteEnter(to, from, next) {
    if (sessionStorage.getItem("token")) {
      next();
    } else {
      next("/login");
    }
  },
  created() {
    this.title = this.$route.params.show || sessionStorage.getItem("useInfo");
    sessionStorage.setItem("useInfo", this.title);
    getCity().then((res) => {
      this.citys = res;
      this.provinces = res;
    });
    getAddress().then((res) => {
      this.addressList = res;
    });
    getUser().then((res) => {
      this.user = res;
    });
  },
  filters: {
    saveNum(val) {
      var reg = /^(\d{3})\d{4}(\d{4})$/;
      return val.replace(reg, "$1****$2");
    },
  },
};
</script>

<style lang="scss">
.main-member {
  background-color: #f2f2f2;
  .row.member {
    .logo {
      padding: 0;
      height: 80px;
      line-height: 80px;
      display: flex;
      align-items: center;
      a {
        img {
          display: block;
          width: 120px;
          height: 33px;
        }
      }
      .title {
        margin-left: 70px;
        margin-right: 10px;
        font-size: 14px;
        color: #909399;
      }
      .el-menu-demo {
        border-width: 0;
        .el-submenu.is-active {
          .el-submenu__title {
            border-width: 0;
          }
        }
      }
    }
    .search.member {
      margin-top: 20px;
      form {
        position: relative;
        height: 40px;

        input {
          width: 412px;
          height: 40px;
          line-height: 40px;
          padding-left: 5px;
          outline: 0;
          color: #757575;
          border: 1px solid #ccc;
          background-color: #fff;
          float: left;
        }

        button {
          height: 40px;
          margin-left: -1px;
          width: 40px;
          cursor: pointer;
          color: #757575;
          border: 1px solid #ccc;
          background-color: #fff;
          float: left;

          .iconfont {
            font-size: 26px;
          }
        }
      }
    }
  }
  .container.order {
    padding-top: 40px;
    padding-bottom: 20px;
    position: relative;
    .menu {
      background-color: #fff;
      width: 250px;
      height: 783px;
      .el-tree {
        padding: 30px 0;
        .el-tree-node__content {
          color: #333;
          height: 40px;
        }
        .el-tree-node__children {
          .el-tree-node__label {
            color: #999;
          }
        }
      }
    }
    .main-content {
      position: absolute;
      top: 40px;
      left: 250px;
      width: calc(100% - 250px);
      height: 783px;
      padding-left: 20px;
      h2 {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
        font-size: 25px;
        font-weight: 400;
      }
      .position-content {
        position: absolute;
        top: 60px;
        left: 20px;
        width: 100%;
        height: calc(100% - 60px);
        background-color: #fff;
        &.el-card__body {
          padding: 10px 20px;
        }
        .addAddress {
          position: absolute;
          right: 0;
          top: -60px;
          line-height: 60px;
          color: #ee0a3b;
          cursor: pointer;
          font-size: 14px;
          &:hover {
            text-decoration: underline;
          }
        }
        .userImg {
          margin-bottom: 20px;
          & > div {
            display: inline-block;
          }
          .img {
            position: relative;
            width: 115px;
            height: 115px;
            img {
              width: 100%;
              height: 100%;
            }
            input[type="file"] {
              opacity: 0;
              position: absolute;
              top: 50%;
              left: 50%;
            }
          }
          .tips {
            margin-left: 20px;
          }
        }
        .el-row {
          .el-col {
            margin-bottom: 22px;
            font-size: 14px;
            .name {
              label {
                display: flex;
                .trueName {
                  width: 92px;
                  text-align: center;
                  font-size: 14px;
                  color: #606266;
                  line-height: 40px;
                  padding: 0 12px 0 0;
                }
                .birthday {
                  width: 92px;
                  text-align: center;
                  font-size: 14px;
                  color: #606266;
                  line-height: 40px;
                  padding: 0 12px 0 0;
                }
              }
            }
            .area {
              .block {
                .demonstration {
                  padding-right: 12px;
                }
              }
            }
            .huxing {
              display: inline-block;
              margin-right: 20px;
            }
            .el-checkbox-group {
              display: inline-block;
            }
            .el-input {
              display: inline-block;
              input {
                display: inline-block;
              }
            }
          }
          .el-col-14 {
            height: 40px;
            line-height: 40px;
          }
        }
        .el-button {
          margin-left: 72px;
          background-color: #2a2a2a;
          color: #fff;
        }
        .el-table {
          .cell {
            text-align: center;
            span {
              cursor: pointer;
              &:hover {
                color: #ee0a3b;
                text-decoration: underline;
              }
            }
          }
        }
        .item {
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding-left: 75px;
          height: 80px;
          border-bottom: 1px solid #ebeef5;
          .left {
            display: flex;
            align-items: center;
            .img {
              margin-right: 52px;
            }
          }
          .right {
            width: 227px;
            text-align: left;
            span {
              cursor: pointer;
              color: #ee0a3b;
            }
          }
        }
      }
    }
  }
  .el-select-dropdown__item {
    color: #606266;
    &.selected {
      color: #263746;
      font-weight: 700;
    }
  }
  .el-dialog__wrapper {
    input {
      display: block;
      width: 100%;
      height: 40px;
      padding-left: 8px;
    }
    .edit-address {
      padding: 0 30px;
      .item {
        position: relative;
        margin-bottom: 25px;
        .flex {
          display: flex;
          justify-content: space-between;
        }
        .tips {
          position: absolute;
          left: 0;
          bottom: -25px;
          width: 100%;
          height: 25px;
          color: #ee0a3b;
        }
        .tips.error::before {
          display: inline-block;
          vertical-align: middle;
          content: "";
          width: 18px;
          height: 20px;
          background: url(../assets/image/register/icons-201703.png) -66px 0 no-repeat;
        }
      }
      .el-select {
        width: 120px;
      }
    }
    .el-dialog__footer {
      text-align: center;
      .el-button {
        background-color: #ee0a3b;
        border: 1px solid #ee0a3b;
        color: #fff;
      }
    }
  }
}
</style>